<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="/js/xlsx.core.min.js"></script>
  <title>Excel模板配置生成</title>
  <style>
    table {
      border: 1px solid #ccc;
      border-collapse: collapse;
      width: 100%;
    }

    table td {
      border: 1px solid #ccc;
      text-align: center;
      height: 30px;
    }

    table tr:first-child {
      background: #ccc;
    }
  </style>
</head>

<body>
  <h2>Excel模板配置生成</h2>
  <input type="file" id="excel" />
  <h3>配置文件</h3>
  <textarea id="output" rows="12" cols="100"></textarea>
</body>

</html>
<script>
  document.querySelector('#excel').onchange = function (e) {
    let file = e.target.files[0];
    readWorkbookFromLocalFile(file, function (workbook) {
      readWorkbook(workbook);
    });
  }

  // 读取本地excel文件，读取Excel文件对象
  function readWorkbookFromLocalFile(file, callback) {
    var reader = new FileReader();
    reader.onload = function (e) {
      var data = e.target.result;
      var workbook = XLSX.read(data, { type: 'binary' });
      console.log(workbook);
      if (callback) callback(workbook);
    };
    reader.readAsBinaryString(file);
  }
  // 转成cvs 要注意Execel表格内容不能包含英文的,不然解析出来的数据格式会有问题
  function readWorkbook(workbook) {
    var sheetNames = workbook.SheetNames; // 工作表名称集合
    var worksheet = workbook.Sheets[sheetNames[0]]; // 这里我们只读取第一张sheet
    console.log(worksheet);

    const fieldMap = Object.keys(worksheet).filter((e) => !e.startsWith('!')).reduce(
      (pre, cur) => {
        if (worksheet[cur].v.toString().startsWith("#")) {
          const matchRes = cur.match(/([a-zA-Z]+)([0-9]+)/)
          const [_,col,row] = matchRes
          pre.push({ col,row, field: worksheet[cur].v.replace("#","") })
        } 
        return pre
      }, []
    )

    console.log(fieldMap)
    var myTextarea = document.getElementById("output");

    // 设置 textarea 的值  
    myTextarea.value = JSON.stringify({
      "Name": "公路压实度(环刀法)",
      "Enabled": true,
      "FileTypeDesc": "excel文件",
      "FileTypeExts": "*.xlsx",
      "IsMulti": true,
      "IsAutoAdd": true,
      "Datas": null,
      "ExcelDataMaps": fieldMap,
      "Graphs": null,
      "Function": null
    }
    )
    // worksheet.forEach(e=>{
    //     console.log(e);
    // })

    // var csv = XLSX.utils.sheet_to_csv(worksheet);
    // console.log(csv);
    // console.log(csv.Sheets["Sheet1"]);

    // csv.Sheets.Sheet1.forEach(e => {
    //     console.log(e)
    // });


    // var rows = csv.split('\n'); // 转化为数组
    // rows.pop(); // 最后一行没用的
    // console.log(rows);  // 打印输出的数组
    // let table = ``;
    // rows.forEach(v => {
    //   let tr = ``;
    //   let td = ``;
    //   v.split(',').forEach(item => {
    //     td += `<td>${item}</td>`
    //   });
    //   tr = `<tr>${td}</tr>`;
    //   table += tr;
    // });
    // table = `<table>${table}</table>`;
    // document.querySelector('.table').innerHTML = table; // 显示table表格
  }
</script>